{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. Uploading data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(one record per member per HH)\n"
     ]
    }
   ],
   "source": [
    "use \"https://github.com/SaoriIwa/Stata-IE-Visual-Library/raw/master/Library/Scatter%20plots/Scatter%20plot%20with%20polynomial%20smoothing%20and%20confidence%20intervals/data.dta\", clear"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. Creating the graph1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "qui sum cons_pae_m_sine, det\n",
    "\n",
    "qui twoway scatter cons_pae_sd_sine cons_pae_m_sine if cons_pae_m_sine < `r(p99)' ///\n",
    "\t|| lpolyci cons_pae_sd_sine cons_pae_m_sine if cons_pae_m_sine < `r(p99)', ///\n",
    "\tlegend(off) /// \n",
    "\txtitle(\" \" \"`=ustrunescape(\"\\u006D\\u0302\")'\", size(large)) /// \t\t\t\t\t\t\t\t\tm-hat\n",
    "\tytitle(\"`=ustrunescape(\"\\u0073\\u0302\")'\" \" \", size(large)) /// \t\t\t\t\t\t\t\t\ts-hat \n",
    "\txlabel(50 \"50\" 100 \"100\" 150 \"150\" 200 \"200\") ///\t\n",
    "\tgraphregion(color(white)) bgcolor(white) ///\n",
    "\tname(s_by_mhat)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. Creating the graph2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "qui sum cons_pae_m_sine, det\n",
    "\n",
    "qui twoway scatter cv cons_pae_m_sine if cons_pae_m_sine<`r(p99)' & cons_pae_m_sine>`r(p1)' ///\n",
    "\t|| lpolyci cv cons_pae_m_sine if cons_pae_m_sine<`r(p99)' & cons_pae_m_sine>`r(p1)', ///\n",
    "\tmcolor(maroon) ///\n",
    "\tytitle(\"`=ustrunescape(\"\\u0073\\u0302/\\u006D\\u0302\")'\" \" \", size(large)) /// \t\t\t\t\ts-hat/m-hat\n",
    "\txtitle(\" \" \"`=ustrunescape(\"\\u006D\\u0302\")'\", size(large)) ///\t\t\t\t\t\t\t\t\tm-hat\n",
    "\tlegend(order(2 3) label(3 \"Local Poly.\") label(2 \"95% CI\")) ///\n",
    "\tgraphregion(color(white)) bgcolor(white) ///\n",
    "\tname(cv_by_mhat)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4. Combining the graphs into one graph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "grc1leg s_by_mhat cv_by_mhat, ///\n",
    "\trow(1) legendfrom(cv_by_mhat) ///\n",
    "\timargin(0 0 0 0) graphregion(margin(t=0 b=0)) ///\n",
    "\tposition(6) fysize(75) fxsize(150) ///\n",
    "\tgraphregion(color(white)) plotregion(color(white))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5. Exporting the graph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "qui graph export \"figure.png\", as(png) replace"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Stata",
   "language": "stata",
   "name": "stata"
  },
  "language_info": {
   "file_extension": ".do",
   "mimetype": "text/x-stata",
   "name": "stata"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
